'''
Created on 2013-11-22

@author: Nich
'''
import unittest
from PyMud.Systems.network_av_system import NetworkAVSystem, NetworkAVNode
from PyMud.messages.message_types import AVMessage
from multiprocessing import Queue
from PyMud.objects.components import Location

from PyMud.objects.components import Senses
    
class NodeFactory():
    def __init__(self, nodes):
        self.nodes = nodes
        
    def create_node_list(self, data):
        return self.nodes

class NetworkAVTest(unittest.TestCase):


    def setUp(self):
        message_template = [([("loudness", 50), ("targeted",), ("is_caller",)], ['You say to {target}, "{text}".']),
                             ([("loudness", 50), ("targeted",), ("is_target",)], ['{player} says to you, "{text}".']),
                             ([("loudness", 50), ("targeted",),],                 ['{player} says to {target}, "{text}".']),
                             ([("loudness", 50), ("is_caller",)],               ['You say, "{text}".']),
                             ([("loudness", 50)],                              ['{player} says, "{text}".']),
                             ([("visibility", 60), ("is_caller")],              ["You can't hear your own voice!"]),          
                                                    
                             ([("visibility", 60), ("is_caller")],             ["{player}'s lips move, but you can't make out what they're saying."])
                             ]
        
        self.AVMessageQueue = Queue()
        self.out_queue = Queue()
        
        #default player, default room
        self.player1 = NetworkAVNode("1", Location("1", "0",  0, 0, 0), Senses("1"))
        #different room, same location
        self.player2 = NetworkAVNode("2", Location("2", "1",  0, 0, 0), Senses("2"))
        #same room, too far away
        self.player3 = NetworkAVNode("3", Location("3", "0",  100, 100, 100), Senses("3"))
        #same room, too far away
        self.player4 = NetworkAVNode("4", Location("4", "0",  15, 15, 15), Senses("4"))
        
        self.player_fact = NodeFactory([self.player1, self.player2, self.player3, self.player4])
        self.netAVSystem = NetworkAVSystem(self.AVMessageQueue, self.player_fact, self.out_queue)
        
        self.message1 = AVMessage("test_message", "test1", Location("5", "0",  0, 0, 0), "1", message_template, target_id = self.player4.id)
        self.message2 = AVMessage("test_message", "test2", Location("6", "0",  0, 0, 0), "4", message_template, target_id = self.player3.id)
        self.message3 = AVMessage("test_message", "test3", Location("7", "0",  0, 0, 0), "1", message_template)
        
        self.AVMessageQueue.put(self.message1)
        self.AVMessageQueue.put(self.message2)                  
        self.AVMessageQueue.put(self.message3)
        
         
    def tearDown(self):
        pass


    def testNetworkAVSystem(self):
        self.netAVSystem.process_AV_messages()
        results = []
        while not self.out_queue.empty():
            results.append(self.out_queue.get())
        
        print(results)
        self.assertEqual(len(results), 6)
        self.assertListEqual(sorted([res.msg for res in results]), sorted(['You say to 4, "test1".', 
                                                            '1 says to you, "test1".',
                                                            'You say, "test3".', 
                                                            '1 says, "test3".',
                                                            '4 says to 3, "test2".',
                                                            'You say to 3, "test2".']))
        

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testAVSystem']
    unittest.main()